#include <my_global.h>
#include <mysql.h>
#include <string.h>
#include "db.h"
#include "configfile.h"

void usage(char * prog);
#define DEBUG(fmt, args...) \
printf("[%s:%d]" fmt "\n", __FUNCTION__, __LINE__ , ##args )
config_file_t *cfile=NULL;
void usage(char * prog)
{
    fprintf(stderr, "usage:%s <options> \n", prog);
    fprintf(stderr, "options:\n"
            "   --createdb   create database\n"
            "   --createtbl     create tables for the database\n"
            "   --dbhost     hostname for the database\n"
            "   --dbname     databasename\n"
            "   --dblogin    db login\n"
            "   --dbpassword db password\n"
            );
}
int createdb_read=0; 
int createtables_read=0; 

int parse_arguments(int argc, char** argv)
{
    int ch, option_index=0;
    const char argstr[]="d:t:s:n:l:p:h";
    int host_read=0; 
    int db_read=0;     
    int login_read=0; 
    int password_read=0; 


    static struct option long_options[] = {
        {"createdb", no_argument, 0, 'd'},        
        {"createtbl", no_argument, 0, 't'},
        {"dbhost", required_argument, 0, 's'},
        {"dbname", required_argument, 0, 'n'},
        {"dblogin", required_argument, 0, 'l'},
        {"dbpassword", required_argument, 0, 'p'},
        {0,0,0,0}
    };

    if (argc < 2) {
         return -1;
    }
    cfile = (config_file_t *) malloc(sizeof(config_file_t));    
    while ((ch=getopt_long(argc, argv, argstr, 
                    long_options, &option_index)) != -1) {
        switch (ch) {
            case 's' : {
                if (optarg)
                    printf("DB Host=%s\n", optarg); 
                strncpy(cfile->db_host,optarg, 100);
                host_read = 1;
                break;
            }
            case 'n' : {
                if (optarg)
                    printf("DB Name=%s\n", optarg); 
                strncpy(cfile->db_name,optarg, 32);
                db_read = 1;
                break;
            }
            case 'l' : {
                if (optarg)
                    printf("DB Login=%s\n", optarg); 
                strncpy(cfile->db_login,optarg, 32);
                login_read=1;
                break;
            }
            case 'p' : {
                if (optarg)
                    printf("DB Password=%s\n", optarg); 
                strncpy(cfile->db_password,optarg, 32);
                password_read=1;
                break;
            }
            case 'd' : {
                if ((host_read==0) ||
                     (db_read==0) ||
                     (login_read==0) ||
                     (password_read==0) )
                {
                    return -1;
                }
                createdb_read=1;
                break;
            }  
            case 't' : {
                if ((host_read==0) ||
                     (db_read==0) ||
                     (login_read==0) ||
                     (password_read==0) )
                {
                    return -1;
                }                    
                createtables_read=1;
                break;
            }             
            
        }
    }
    free(cfile);
    return 1;
}

int main(int argc, char **argv)
{
  int ret;

  ret = parse_arguments(argc, argv);
  if (ret < 0) {
      usage(argv[0]);
      return ret;
  }
  
  init_params(DB_TYPE_MASTER);
  if (createtables_read == 1) {
     ret = createTables();
  } else 
  if (createdb_read == 1) {
     ret = createDb();
  } else  {
      usage(argv[0]);
  }
  return ret;
}
